﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Krile.Kernel;
using System.Xml.Serialization;

namespace Twinkle.Data
{
    public class Config
    {
        public static Config LoadConfig()
        {
            var t = Path.Combine(Bridge.Config.PluginPath, Define.ConfigFile);
            if (File.Exists(t))
            {
                try
                {
                    return Bridge.CommonOperation.XMLLoad<Config>(t);
                }
                catch (Exception e)
                {
                    Bridge.DebugReporter.AddReport(e);
                }
            }
            return new Config();
        }

        public void SaveConfig()
        {
            var t = Path.Combine(Bridge.Config.PluginPath, Define.ConfigFile);
            Bridge.CommonOperation.XMLSave<Config>(this, t);
        }

        public Account[] Accounts = null;

        public int StatusCountPerRecv = 40;

        public bool GradualReceive = true;

        public bool RecvFavTimeline = false;

        public bool EnableBetaOperations = false;

        public StatusDesignConfig StatusDesign = new StatusDesignConfig();
        public class StatusDesignConfig
        {
            public bool ShowRTButton = true;

            public bool ShowUnofficialRTButton = true;

            public bool ShowQTButton = true;

            public bool RemoveSpaceRT = true;

            public bool ShowIDAlternative = false;

            [XmlIgnore()]
            public System.Drawing.Color DMBackColor = System.Drawing.Color.Red;

            [XmlIgnore()]
            public System.Drawing.Color DMTextColor = System.Drawing.Color.White;

            public string DMBColorHTML
            {
                get { return System.Drawing.ColorTranslator.ToHtml(DMBackColor); }
                set { DMBackColor = System.Drawing.ColorTranslator.FromHtml(value); }
            }

            public string DMTColorHTML
            {
                get { return System.Drawing.ColorTranslator.ToHtml(DMTextColor); }
                set { DMTextColor = System.Drawing.ColorTranslator.FromHtml(value); }
            }

        }

        public ExternalConfig External = new ExternalConfig();
        public class ExternalConfig
        {
            public URLShortenServiceDescriptor URLShoteningService = new URLShortenServiceDescriptor();

            public class URLShortenServiceDescriptor
            {
                public bool Use = false;

                public bool UseOnlyExceeds = true;

                public enum URLShortenServiceProviders { TinyURL, bitly, jmp }

                public URLShortenServiceProviders Provider = URLShortenServiceProviders.jmp;

                public string[] GetServicesStrings()
                {
                    return new[] { "TinyURL.com", "bit.ly", "j.mp" };
                }

                public string GetString(URLShortenServiceProviders svc)
                {
                    switch (svc)
                    {
                        case URLShortenServiceProviders.bitly:
                            return "bit.ly";
                        case URLShortenServiceProviders.jmp:
                            return "j.mp";
                        case URLShortenServiceProviders.TinyURL:
                            return "TinyURL.com";
                        default:
                            throw new ArgumentException();
                    }
                }

                public bool UseAsJIT = false;

                public string ID = String.Empty;

                [XmlIgnore()]
                public string PW = String.Empty;

                public string PWEncrypted
                {
                    get { return Bridge.CommonOperation.AESEncrypt(this.PW); }
                    set
                    {
                        try
                        {
                            this.PW = Bridge.CommonOperation.AESDecrypt(value);
                        }
                        catch (Exception e)
                        {
                            Bridge.DebugReporter.AddReport(e);
                        }
                    }
                }

                public bool AutoResolveURL = true;
            }

            public ImageEmbeddingServiceDescriptor ImageEmbeddingService = new ImageEmbeddingServiceDescriptor();

            public class ImageEmbeddingServiceDescriptor
            {
                public bool UseImageEmbedding = false;

                public enum ImageEmbeddingServices { TwitPic = 0, Posterous = 1, Gyazo = 2 }

                public ImageEmbeddingServices EmbeddingService = ImageEmbeddingServices.Gyazo;

                public string ID = String.Empty;

                [XmlIgnore()]
                public string PW = String.Empty;

                public string PWEncrypted
                {
                    get { return Bridge.CommonOperation.AESEncrypt(this.PW); }
                    set
                    {
                        try
                        {
                            this.PW = Bridge.CommonOperation.AESDecrypt(value);
                        }
                        catch (Exception e)
                        {
                            Bridge.DebugReporter.AddReport(e);
                        }
                    }

                }

                public bool ExtractEmbedded = true;
            }
        }

        public BehaviorConfig Behavior = new BehaviorConfig();
        public class BehaviorConfig
        {

            public bool UseSuggest = true;

            public bool ConfirmAccountsBeforePost = true;

            public bool ConfirmAccountsBeforeRT = true;

            public bool ConfirmAccountsBeforeFav = true;

            public bool TreatAsReplyToMeIfMentionToMe = true;

            public bool DontUseSignatureWhenContainsRT = true;

            public bool DontUseSignatureWhenContainsReply = true;

            public bool AutoLookupRTOrigin = true;

            public bool AddHashtagBeforeRT = true;

            public bool HashtagOpenInKrile = false;

            public bool UseTagAutoBinding = true;

            public TagBinding[] TagAutoBindingData = null;

            public string PreviousStaticBindingHashtag = null;

            public class TagBinding
            {
                public string Keyword { get; set; }
                public enum BindingType
                {
                    Contains,
                    StartsWith,
                    EndsWith,
                    Equals,
                    Regex
                }
                public BindingType Type { get; set; }
                public string Tags { get; set; }
            }
        }
    }
}
